package com.easybbs.config;

import cn.dev33.satoken.interceptor.SaInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @Author: yudaxian
 * @Description: 路由拦截器配置
 * @DateTime: 2024/10/18 9:42
 **/
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    String[] excludePatterns = new String[]{"/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**",
            "/api", "/api/error", "/api-docs", "/api-docs/**", "/doc.html/**", "/api/favicon.ico", "/favicon.ico", "/META-INF/resources/"};


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册 Sa-Token 拦截器，打开注解式鉴权功能
        registry.addInterceptor(new SaInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns(excludePatterns)
        ;
    }

    /**
     * 注册静态资源映射 knife4j 文档
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        //favicon.ico
        registry.addResourceHandler("/favicon.ico").addResourceLocations("classpath:/static/");
    }
}
